package org.angelica.config;

import org.angelica.common.filter.JwtTokenFilter;
import org.angelica.common.filter.SignaureFilter;
import org.angelica.core.xss.XssFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;

/**
 * 配置Filter的执行顺序
 * @author aizhimin
 */
@Configuration
public class FilterConfig {

	@Bean
    public FilterRegistrationBean<SignaureFilter> signaureFilterRegistration() {
        FilterRegistrationBean<SignaureFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new SignaureFilter());
        registration.addUrlPatterns("/*");
        registration.setName("signaureFilter");
        registration.setOrder(Integer.MAX_VALUE - 2);
        return registration;
    }
	
    @Bean
    public FilterRegistrationBean<JwtTokenFilter> jwtTokenFilterRegistration() {
        FilterRegistrationBean<JwtTokenFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new JwtTokenFilter());
        registration.addUrlPatterns("/*");
        registration.setName("jwtTokenFilter");
        registration.setOrder(Integer.MAX_VALUE - 1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns("/*");
        registration.setName("xssFilter");
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }
}
